
import { put, takeEvery } from 'redux-saga/effects'
import apiType, {
    GET_ALL_PROMOTION_WITH_STATS,
    GET_ALL_COUPONS,
    GET_COUPONS,
    GET_COUPON_DETAIL,
    CREATE_COUPONS,
    UPDATE_COUPONS,
    PAUSE_COUPON,
    RESUME_COUPON,
    PUBLISH_COUPON,
    GET_COUPONS_DISTRIBUTION_STATS,
    SET_COUPONS_DISTRIBUTION,
    GET_PROMOTION,
    GET_PROMOTION_DETAIL,
    CREATE_PROMOTION,
    UPDATE_PROMOTION,
    PAUSE_PROMOTION,
    RESUME_PROMOTION,
    PUBLISH_PROMOTION,
} from "@constants/ActionTypes";
import { deleteAxios, getAxios, postAxios, putAxios } from "@utils/api";

// 列出促销活动及其统计
export const getPromotionStats = (params, callback) => {
    return {
        type: apiType(GET_ALL_PROMOTION_WITH_STATS).request,
        params,
        callback
    }
}
// 列出优惠券
export const getAllCoupons = (params, callback) => {
    return {
        type: apiType(GET_ALL_COUPONS).request,
        params,
        callback
    }
}
// 列出优惠券
export const getCoupons = (params, callback) => {
    return {
        type: apiType(GET_COUPONS).request,
        params,
        callback
    }
}
// 获取优惠券
export const getCouponDetail = (params, callback) => {
    return {
        type: apiType(GET_COUPON_DETAIL).request,
        params,
        callback
    }
}
// 新建优惠券
export const createCoupons = (params, callback) => {
    return {
        type: apiType(CREATE_COUPONS).request,
        params,
        callback
    }
}
// 更新优惠券
export const updateCoupons = (params, callback) => {
    return {
        type: apiType(UPDATE_COUPONS).request,
        params,
        callback
    }
}
// 暂停优惠券
export const pauseCoupons = (params, callback) => {
    return {
        type: apiType(PAUSE_COUPON).request,
        params,
        callback
    }
}
// 发布优惠券
export const publishCoupons = (params, callback) => {
    return {
        type: apiType(PUBLISH_COUPON).request,
        params,
        callback
    }
}
// 恢复优惠券
export const resumeCoupons = (params, callback) => {
    return {
        type: apiType(RESUME_COUPON).request,
        params,
        callback
    }
}
// 列出优惠券投放及其统计
export const getCouponDistributionWithStats = (params, callback) => {
    return {
        type: apiType(GET_COUPONS_DISTRIBUTION_STATS).request,
        params,
        callback
    }
}
// 新增投放
export const setCouponDistribution = (params, callback) => {
    return {
        type: apiType(SET_COUPONS_DISTRIBUTION).request,
        params,
        callback
    }
}

// 列出促销活动
export const getPromotion = (params, callback) => {
    return {
        type: apiType(GET_PROMOTION).request,
        params,
        callback
    }
}
// 获取促销活动
export const getPromotionDetail = (params, callback) => {
    return {
        type: apiType(GET_PROMOTION_DETAIL).request,
        params,
        callback
    }
}
// 新建促销活动
export const createPromotion = (params, callback) => {
    return {
        type: apiType(CREATE_PROMOTION).request,
        params,
        callback
    }
}
// 更新促销活动
export const updatePromotion = (params, callback) => {
    return {
        type: apiType(UPDATE_PROMOTION).request,
        params,
        callback
    }
}
// 暂停促销活动
export const pausePromotion = (params, callback) => {
    return {
        type: apiType(PAUSE_PROMOTION).request,
        params,
        callback
    }
}
// 发布促销活动
export const publishPromotion = (params, callback) => {
    return {
        type: apiType(PUBLISH_PROMOTION).request,
        params,
        callback
    }
}
// 恢复促销活动
export const resumePromotion = (params, callback) => {
    return {
        type: apiType(RESUME_PROMOTION).request,
        params,
        callback
    }
}

const sagas = {
    *[apiType(GET_ALL_PROMOTION_WITH_STATS).request]({ params, callback }) {
        try {
            const response = yield getAxios('/market/promotionWithStats', params)
            yield put({
                type: apiType(GET_ALL_PROMOTION_WITH_STATS).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_ALL_PROMOTION_WITH_STATS).failure });
        }
    },
    *[apiType(GET_ALL_COUPONS).request]({ params, callback }) {
        try {
            const response = yield getAxios('/market/couponWithStats', params)
            yield put({
                type: apiType(GET_ALL_COUPONS).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_ALL_COUPONS).failure });
        }
    },
    *[apiType(GET_COUPONS).request]({ params, callback }) {
        try {
            const response = yield getAxios('/market/coupons', params)
            yield put({
                type: apiType(GET_COUPONS).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_COUPONS).failure });
        }
    },
    *[apiType(CREATE_COUPONS).request]({ params, callback }) {
        try {
            const response = yield postAxios('/market/coupon', params)
            yield put({
                type: apiType(CREATE_COUPONS).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(CREATE_COUPONS).failure });
        }
    },
    *[apiType(UPDATE_COUPONS).request]({ params, callback }) {
        try {
            const response = yield putAxios('/market/coupon', params)
            yield put({
                type: apiType(UPDATE_COUPONS).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(UPDATE_COUPONS).failure });
        }
    },
    *[apiType(GET_COUPON_DETAIL).request]({ params, callback }) {
        try {
            const response = yield getAxios(`/market/coupon/${params.id}`)
            yield put({
                type: apiType(GET_COUPON_DETAIL).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_COUPON_DETAIL).failure });
        }
    },
    *[apiType(PAUSE_COUPON).request]({ params, callback }) {
        try {
            const response = yield putAxios(`/market/coupon/${params.id}/pause`)
            yield put({
                type: apiType(PAUSE_COUPON).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(PAUSE_COUPON).failure });
        }
    },
    *[apiType(RESUME_COUPON).request]({ params, callback }) {
        try {
            const response = yield putAxios(`/market/coupon/${params.id}/resume`)
            yield put({
                type: apiType(RESUME_COUPON).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(RESUME_COUPON).failure });
        }
    },
    *[apiType(PUBLISH_COUPON).request]({ params, callback }) {
        try {
            const response = yield putAxios(`/market/coupon/${params.id}/publish`)
            yield put({
                type: apiType(PUBLISH_COUPON).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(PUBLISH_COUPON).failure });
        }
    },
    *[apiType(GET_COUPONS_DISTRIBUTION_STATS).request]({ params, callback }) {
        try {
            const response = yield getAxios('/market/coupon_distributionWithStats',params)
            yield put({
                type: apiType(GET_COUPONS_DISTRIBUTION_STATS).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_COUPONS_DISTRIBUTION_STATS).failure });
        }
    },
    *[apiType(SET_COUPONS_DISTRIBUTION).request]({ params, callback }) {
        try {
            const response = yield postAxios('/market/coupon_distribution',params)
            yield put({
                type: apiType(SET_COUPONS_DISTRIBUTION).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(SET_COUPONS_DISTRIBUTION).failure });
        }
    },
    *[apiType(GET_PROMOTION).request]({ params, callback }) {
        try {
            const response = yield getAxios('/market/promotionWithStats', params)
            yield put({
                type: apiType(GET_PROMOTION).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_PROMOTION).failure });
        }
    },
    *[apiType(CREATE_PROMOTION).request]({ params, callback }) {
        try {
            const response = yield postAxios('/market/promotion', params)
            yield put({
                type: apiType(CREATE_PROMOTION).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(CREATE_PROMOTION).failure });
        }
    },
    *[apiType(UPDATE_PROMOTION).request]({ params, callback }) {
        try {
            const response = yield putAxios('/market/promotion', params)
            yield put({
                type: apiType(UPDATE_PROMOTION).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(UPDATE_PROMOTION).failure });
        }
    },
    *[apiType(GET_PROMOTION_DETAIL).request]({ params, callback }) {
        try {
            const response = yield getAxios(`/market/promotion/${params.id}`)
            yield put({
                type: apiType(GET_PROMOTION_DETAIL).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(GET_PROMOTION_DETAIL).failure });
        }
    },
    *[apiType(PAUSE_PROMOTION).request]({ params, callback }) {
        try {
            const response = yield putAxios(`/market/promotion/${params.id}/pause`)
            yield put({
                type: apiType(PAUSE_PROMOTION).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(PAUSE_PROMOTION).failure });
        }
    },
    *[apiType(RESUME_PROMOTION).request]({ params, callback }) {
        try {
            const response = yield putAxios(`/market/promotion/${params.id}/resume`)
            yield put({
                type: apiType(RESUME_PROMOTION).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(RESUME_PROMOTION).failure });
        }
    },
    *[apiType(PUBLISH_PROMOTION).request]({ params, callback }) {
        try {
            const response = yield putAxios(`/market/promotion/${params.id}/publish`)
            yield put({
                type: apiType(PUBLISH_PROMOTION).success,
                response,
            });
            callback && callback(response)
        } catch (e) {
            yield put({ type: apiType(PUBLISH_PROMOTION).failure });
        }
    },
}


export default Object.keys(sagas).map(item => {
    return function* s() {
        yield takeEvery(item, function* (args) {
            try {
                yield sagas[item](args)
            } catch (e) {
                console.log(e)
            }
        })
    }
})